import 'package:date_format/date_format.dart';
import 'package:ycbl/common/bl_live_status.dart';

class LearnTaskListEntity {
  int ret;
  LearnTaskRows data;
  String msg;

  LearnTaskListEntity({this.ret, this.data, this.msg});

  LearnTaskListEntity.fromJson(Map<String, dynamic> json) {
    ret = json['ret'];
    msg = json['msg'];
    data = json['data'] != null ? new LearnTaskRows.fromJson(json['data']) : null;
  }
}

class LearnTaskRows {
  int total;
  List<LearnTaskEntity> rows;

  LearnTaskRows({this.total, this.rows});

  LearnTaskRows.fromJson(Map<String, dynamic> json) {
    total = json['total'];
    if (json['rows'] != null) {
      rows = new List<LearnTaskEntity>();
      json['rows'].forEach((v) {
        rows.add(new LearnTaskEntity.fromJson(v));
      });
    }
  }
}

class LearnTaskEntity {
  ///任务名称
  String courseName;
  ///短开始、结束时间 19:00
  String shortStartTme;
  String shortEndTime;
  ///长开始、结束时间 2020-07-17 19:00:00
  DateTime startDateTime;
  DateTime endDateTime;
  ///展示互动直播 Url 和 token
  String stuUrl;
  String liveToken;
  ///展示互动录播 Url 和 token
  String vodUrl;
  String vodToken;
  /// class In 相关
  String lessonKey;
  String classInRecordUrl;
  String classInCourseId;
  String classInClassId;

  //任务描述
  String subDesc = '';

  LearnTaskEntity({
    this.courseName,
    this.shortStartTme,
    this.shortEndTime,
    this.endDateTime,
    this.startDateTime,
    this.vodUrl,
    this.liveToken,
    this.stuUrl,
    this.vodToken,
    this.lessonKey,
    this.classInRecordUrl,
    this.classInCourseId,
    this.classInClassId
  });

  LearnTaskEntity.fromJson(Map<String, dynamic> json) {
    courseName = json['COURSENAME'] ?? json['COURSE_NAME'];
    shortStartTme = json['CLASS_BEGIN_TIME'];
    shortEndTime = json['CLASS_END_TIME'];
    // 2020-07-17 19:00:00
    var longStartTime = json['COURSE_START_TIME'] ?? json['COURSE_START_DATE'];
    startDateTime = DateTime.parse(longStartTime);
    var longEndTime = json['COURSE_END_TIME'];
    endDateTime = DateTime.parse(longEndTime);
    //格式化日期
    subDesc = formatSubDesc();
    vodUrl = json['SPHFDZ'];
    liveToken = json['STU_CUSTOMER_TOKEN'];
    stuUrl = json['STU_URL'];
    vodToken = json['STU_WEB_TOKEN'];
    lessonKey = json['LESSONKEY'];
    classInRecordUrl = json['CLASSIN_RECORD_URL'];
    classInCourseId = json['CLASSIN_COURSE_ID'];
    classInClassId = json['CLASSIN_CLASS_ID'];
  }

  /// 判断是否为展示互动
  bool isClassIn() {
    return lessonKey != null && lessonKey.isNotEmpty;
  }

  String formatSubDesc() {
    return '${formatDate(startDateTime, ['mm', '.', 'dd', ' ', 'HH', ':', 'nn'])}-${formatDate(endDateTime, ['HH', ':', 'nn'])}';
  }

  /// 获取直播状态
  BLLiveStatus liveStatus() {
    var now = DateTime.now();
    if (now.isBefore(startDateTime)) {
      return BLLiveStatus.NOT_START;
    }else if (now.isBefore(endDateTime)) {
      return BLLiveStatus.LIVING;
    }else if (vodUrl != null || classInRecordUrl != null ) {
      return BLLiveStatus.REPLAY;
    }else {
      return BLLiveStatus.FINISH;
    }
  }

}
